<template>
  <div style="width: 100%; height: 100%; position: relative">
    <div :id="idname" style="width: 100%; height: 100%" v-if="!isShow"></div>
    <div class="no_data" v-show="isShow">
      <img ref="img" src="@/assets/images/no_data.png" alt="" />
      <p>暂无数据</p>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import { EleResize } from "@/utils/esresize.js";
export default {
  props: {
    idname: {
      type: String,
      requied: true,
    },
    datalist: {
      type: Object,
      default: {
        title: [],
        data: [],
        series: {
          name: "demo",
        },
        unit: "%",
      },
    },
    // 百分号
    percent: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isShow: false,
      myChart: "",
      img: "",
    };
  },
  watch: {
    datalist: {
      handler(val, oldVal) {
        if (val.data && val.data.length) {
          this.$nextTick(() => {
            this.chartColumn(1);
          });
        } else {
          this.$nextTick(() => {
            this.chartColumn(2);
          });
        }
      },
      deep: true,
    },
  },
  methods: {
    // 只适用图表方法
    drawLine(dom, option) {
      if (document.getElementById(dom)) {
        let myChart = echarts.init(document.getElementById(dom));
        this.myChart = myChart;
        let resizeDiv = document.getElementById(dom);
        myChart.clear();
        myChart.setOption(option);
        let listener = function () {
          myChart.resize();
        };
        EleResize.on(resizeDiv, listener);
      }
    },
    chartColumn(type) {
      // 折线图
      let dom = this.idname;
      let option1 = {
        graphic: [
          {
            type: "image",
            id: "logo",
            left: "center",
            top: "50",
            bounding: "raw",
            style: {
              image: this.$refs["img"].currentSrc,
              width: 92,
              // height: 99,
              opacity: 1,
            },
          },
        ],
      };
      let option2 = {
        tooltip: {
          show: true,
          trigger: "axis",
          formatter: "{a} <br/>{b} : {c}",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#03A98E",
            },
          },
        },
        grid: {
          top: 20,
          bottom: 40,
          left: 60,
          right: 40,
        },
        xAxis: {
          type: "category",
          splitLine: {
            show: false,
            lineStyle: {
              color: "#eaeaea",
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#333",
            },
          },
          axisTick: {
            show: false,
          },
          data: this.datalist.title,
        },
        yAxis: {
          splitLine: {
            show: true,
            lineStyle: {
              color: "#ccc",
              type: "dashed",
            },
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#333",
            },
          },
          axisLine: {
            lineStyle: {
              color: "rgba(107,107,107,0.37)",
            },
          },
          axisTick: {
            show: false,
          },
          type: "value",
        },
        series: [
          {
            name: this.datalist.series.name,
            data: this.datalist.data,
            type: "bar",
            barWidth: 20,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "#6DF0DA",
                    },
                    {
                      offset: 1,
                      color: "#05AA8F",
                    },
                  ],
                  false
                ),
              },
            },
          },
        ],
      };
      let option = type == 1 ? option2 : option1;
      this.drawLine(dom, option, type);
    },
  },
  mounted() {
    this.chartColumn();
  },
  updated() {
    // this.chartColumn();
  },
};
</script>

<style lang="scss" scoped>
.no_data {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  img {
    display: block;
    width: 92px;
    height: 99px;
  }
  p {
    font-size: 12px;
    padding: 15px 0 0 0;
    transform: translateX(-12px);
  }
}
</style>
